package com.scalar.chiptrack.operations.shippingRequest.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.shippingRequest.forms.ShippingRequestForm;
import com.scalar.chiptrack.operations.shippingRequest.ShippingRequest;
import com.scalar.chiptrack.operations.shippingRequest.dao.ShippingRequestDAO;
import com.scalar.chiptrack.operations.shippingRequest.actionCommands.ShippingRequestCommandFactory;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Calendar;



public class ShippingRequestAction extends ChipTrackAction
{
	public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
        HashMap errors  = new HashMap();
        HashMap info    = new HashMap();
		ActionForward actionForward = mapping.findForward( "search" );
        String action = request.getParameter( Constants.REQUEST_PARAM_ACTION );
        boolean addRequestAttribs = true;


        ShippingRequestForm shippingRequestForm = (ShippingRequestForm)form;
        try
        {

            if( ( action == null ) || ( action.trim().length() ==0 ) )
            {
                Logger.debug( "No Action specified. Forwarding to the search page" );
                ShippingRequest shippingRequest = new ShippingRequest();
                Calendar cl   	= Calendar.getInstance();
                shippingRequest.setYear((cl.get( Calendar.YEAR )));
                shippingRequestForm.setShippingRequest( shippingRequest );
                addRequestAttribs = true;
            }
            else
            {
                try
                {
                    ActionCommand shippingRerquestCommand = ShippingRequestCommandFactory.getActionCommand( action );

                    if( shippingRerquestCommand == null )
                    {
                        Logger.debug( "Could not retreive the corresponding action command" );
                        errors.put( "Invalid request", null );
                    }
                    else
                    {
                        actionForward = shippingRerquestCommand.execute( mapping, form, request, response );
                        addRequestAttribs = false;
                    }
                }
                catch( Exception e )
                {
                    Logger.error( "Error while executing the action command", e );
                }
            }
        }
		catch( Exception e )
		{
            Logger.error( "Error while processing the request", e );
			errors.put( "Could not load the Shipping Request Information" , null );
            e.printStackTrace();
		}

        if( addRequestAttribs )
        {
            request.setAttribute( "vendorList", ShippingRequestDAO.loadClassStroeLocations( null )  );
            request.setAttribute( "partNumberList", ShippingRequestDAO.getProductList( Constants.LOCATION_TYPE_CLASS_STORE ) );
            request.setAttribute( "yearList", ShippingRequestDAO.getYearList() );
            request.setAttribute( "statusList", ShippingRequestDAO.getStatusList() );
            request.setAttribute( SessionKeys.INFO_MESSAGES, info );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        }


        return actionForward;

    }

}